<?
namespace xto\data\service;

use think\Cache;
use xto\data\model\AppConfigModel;
use xto\data\entities\ConfigInfo;
use xto\data\Sql;
use xto\App;

class ConfigService{
	static function save($config){
		$data=[
			'name' 	=> $config['name'],
			'value' => $config['value'], 
			'appid' => App::appid()
			];
		$map['appid'] 	=App::appid();
		$map['name'] 	=$config['name'];
		return Sql::saveorupdate('xto_config',$data,$map);
	}

	static function getconfig($name){
		$map['appid'] 	=App::appid();
		$map['name'] 	=$name;
		return Sql::getfield('xto_config',$map,'value');
	}

	static function getConfigs($iscache=false){
		$n=App::instance()->config_cache_name();
		if($iscache){
			$result=Cache::get($n);
			if (!empty($result)) {
				return $result;
			}
		} 
		$map['appid'] 	=App::appid();
		
		$result=Sql::select('xto_config',$map);
		$new=[];
		foreach ($result as $key => $value) {
		 	$new[$value['name']]=$value['value'];
		 } 
		if($iscache){
			Cache::set($n,$new,3600);
		} 
		return $new;
	}
}